package cgl.hadoopsensorgrid.sensorgrid.util;

import java.io.*;
import java.util.*;

/***
 * 
 * Holds methods for property file handling.
 *
 * @author <a href="mailto:obalsoy@indiana.edu">Ozgur Balsoy</a>
 * @version 0.0.1
 * 
 */
public class PropertyFile {

  public PropertyFile() {
  }

	/**
	 * Returns the named file as a Property object.
	 * 
	 * @param fileName
	 *          name of the file. Either full, or relative to the current path, or
	 *          must exist in the classpath or a JAR file.
	 * @return properties
	 * @throws NullPointerException
	 *           if file name is null.
	 * @throws Exception
	 *           if file cannot be found or any other IO errors occur.
	 */
  public static Properties loadProperties(String fileName) {
    Properties p = null;
    if (fileName == null) {
      throw new NullPointerException("File name cannot be null.");
    }
    try {
      File file = new File(fileName);
      if (file.exists()) {
        p = new Properties();
        p.load(new FileInputStream(file));
        return p;
      }
    }
    catch (IOException ioe) {
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    try {
      InputStream is = PropertyFile.class.getResourceAsStream("/" + fileName);
      if (is != null) {
        p = new Properties();
        p.load(is);
      }
    }
    catch (Exception e) {
      System.out.println("Properties file not found in classpath.");
      e.printStackTrace();
    }
    return p;
  }

}
